package com.cloud.otherThreads;

import java.io.IOException;
import java.util.Date;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import com.cloud.entity.ResultVo;
import com.cz073586.tools.http.HttpManagerUtil;
import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.Log4jUtil;
import com.cz073586.tools.util.Tools;
import com.local.common.LogInfoConstan;
import com.local.entity.vo.ReportCarInfoVo;
import com.local.manager.cfgManager.reportConfigManager;
import com.local.model.Log;
import com.local.service.face.LogService;
import com.local.util.HttpClientUtil;

/**
 * 类描述: 上报车牌信息到  公安
 * 
 * @ClassName: UpDateCrossingRunble
 * @author Administrator
 * @date 2015年10月10日 下午7:45:03
 */
public class UploadReportCarInfoRunble implements Runnable
{
	
	private Log4jUtil log = Log4jUtil.getLog4j(UploadReportCarInfoRunble.class);
	private ReportCarInfoVo reportCarInfoVo;
	private LogService logServier;
	
	public UploadReportCarInfoRunble(
			ReportCarInfoVo reportCarInfoVo,
			LogService logServier)
	{
		super();
		this.reportCarInfoVo = reportCarInfoVo;
		this.logServier = logServier;
	}
	
	@Override
	public void run()
	{
		// 发送请求
		CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
				.getHttpClient(null);
		try
		{
			
			if (Tools.isEmpty(this.reportCarInfoVo))
			{
				return;
			}
			// 获取请求路径
			String uri = reportConfigManager.instance().getHost()
					+ reportConfigManager.instance().getCfgValueByKey(
							"reportCarInfo");
			
			HttpPost post = new HttpPost(uri);
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			String sendData = JsonUtil.getJsonString(reportCarInfoVo);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			post.setEntity(s);
			
			log.debug("上报车牌系统【信息】"+sendData);
			
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity(), "UTF-8");
			
			log.debug("上报车牌系统【返回】"+result);
			
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			
			if (resultVo.getResult().equals("success"))
			{
				System.out.println("上报车牌系统成功");
			} else
			{
				System.out.println("上报车牌系统失败.");
				addErrorLog(this.reportCarInfoVo.getUuid(),
						"上报车牌系统失败.",reportCarInfoVo.getPassType());
			}
			
		} catch (Throwable t)
		{
			log.error("", t);
			addErrorLog(this.reportCarInfoVo.getUuid(), "上报车牌系统失败.",reportCarInfoVo.getPassType());
			try
			{
				if (httpClient != null)
				{
					httpClient.close();
				}
			} catch (IOException localIOException1)
			{
			}
		} finally
		{
			try
			{
				if (httpClient != null)
				{
					httpClient.close();
				}
			} catch (IOException localIOException2)
			{
			}
		}
	}
	
	public void addErrorLog(String businessUuid, String deScript,Integer passType)
	{
		Log logInfo = new Log();
		logInfo.setCreaterAt(new Date());
		logInfo.setSummary("上报车辆异常.");
		//0 进场  1:出场
		logInfo.setDescript(passType.toString());
		logInfo.setLevel(LogInfoConstan.LEVEL_ALERT);
		logInfo.setSrc(LogInfoConstan.SRC_SYSTEM);
		logInfo.setStatus(LogInfoConstan.STATUS_UNTREATED);
		logInfo.setCls("com.cloud.threads.UploadReportCarInfoRunble");
		logInfo.setModule(LogInfoConstan.REPORT_CAR_INFO);
		
		logInfo.setBusinessUuid(businessUuid);
		logServier.addLogInfo(logInfo);
		
	}
}
